home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / Fl_Clock.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-25  |  4.9 KB  |  175 lines

  1. //
  2. // "$Id: Fl_Clock.cxx,v 1.8.2.1 1999/03/25 15:26:42 mike Exp $"
  3. //
  4. // Clock widget for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. #include <FL/Fl.H>
  27. #include <FL/Fl_Clock.H>
  28. #include <FL/fl_draw.H>
  29. #include <math.h>
  30. #include <time.h>
  31. #ifndef WIN32
  32. #  include <sys/time.h>
  33. #endif /* !WIN32 */
  34.  
  35. // Original clock display written by Paul Haeberli at SGI.
  36. // Modifications by Mark Overmars for Forms
  37. // Further changes by Bill Spitzak for fltk
  38.  
  39. const float hourhand[4][2] = {{-0.5f, 0}, {0, 1.5f}, {0.5f, 0}, {0, -7.0f}};
  40. const float  minhand[4][2] = {{-0.5f, 0}, {0, 1.5f}, {0.5f, 0}, {0, -11.5f}};
  41. const float  sechand[4][2] = {{-0.1f, 0}, {0, 2.0f}, {0.1f, 0}, {0, -11.5f}};
  42.  
  43. static void drawhand(double ang,const float v[][2],Fl_Color fill,Fl_Color line)
  44. {
  45.   fl_push_matrix();
  46.   fl_rotate(ang);
  47.   fl_color(fill); fl_begin_polygon();
  48.   int i; for (i=0; i<4; i++) fl_vertex(v[i][0],v[i][1]); fl_end_polygon();
  49.   fl_color(line); fl_begin_loop();
  50.   for (i=0; i<4; i++) fl_vertex(v[i][0],v[i][1]); fl_end_loop();
  51.   fl_pop_matrix();
  52. }
  53.  
  54. void Fl_Clock_Output::drawhands(Fl_Color fill, Fl_Color line) {
  55.   drawhand(-360*(hour()+minute()/60.0)/12, hourhand, fill, line);
  56.   drawhand(-360*(minute()+second()/60.0)/60, minhand, fill, line);
  57.   drawhand(-360*(second()/60.0), sechand, fill, line);
  58. }
  59.  
  60. static void rect(double x, double y, double w, double h) {
  61.   double r = x+w;
  62.   double t = y+h;
  63.   fl_begin_polygon();
  64.   fl_vertex(x, y);
  65.   fl_vertex(r, y);
  66.   fl_vertex(r, t);
  67.   fl_vertex(x, t);
  68.   fl_end_polygon();
  69. }
  70.  
  71. void Fl_Clock_Output::draw(int x, int y, int w, int h) {
  72.   draw_box(box(), x, y, w, h, type()==FL_ROUND_CLOCK ? FL_GRAY : color());
  73.   fl_push_matrix();
  74.   fl_translate(x+w/2.0-.5, y+h/2.0-.5);
  75.   fl_scale((w-1)/28.0, (h-1)/28.0);
  76.   if (type() == FL_ROUND_CLOCK) {
  77.     fl_color(color());
  78.     fl_begin_polygon(); fl_circle(0,0,14); fl_end_polygon();
  79.     fl_color(FL_BLACK);
  80.     fl_begin_loop(); fl_circle(0,0,14); fl_end_loop();
  81.   }
  82.   // draw the shadows:
  83.   fl_push_matrix();
  84.   fl_translate(0.60, 0.60);
  85.   drawhands(FL_DARK3, FL_DARK3);
  86.   fl_pop_matrix();
  87.   // draw the tick marks:
  88.   fl_push_matrix();
  89.   fl_color(FL_BLACK); // color was 52
  90.   for (int i=0; i<12; i++) {
  91.     if (i==6) rect(-0.5, 9, 1, 2);
  92.     else if (i==3 || i==0 || i== 9) rect(-0.5, 9.5, 1, 1);
  93.     else rect(-0.25, 9.5, .5, 1);
  94.     fl_rotate(-30);
  95.   }
  96.   fl_pop_matrix();
  97.   // draw the hands:
  98.   drawhands(selection_color(), FL_GRAY0); // color was 54
  99.   fl_pop_matrix();
  100. }
  101.  
  102. void Fl_Clock_Output::draw() {
  103.   draw(x(), y(), w(), h());
  104.   draw_label();
  105. }
  106.  
  107. void Fl_Clock_Output::value(int h, int m, int s) {
  108.   if (h!=hour_ || m!=minute_ || s!=second_) {
  109.     hour_ = h; minute_ = m; second_ = s;
  110.     damage(FL_DAMAGE_CHILD);
  111.   }
  112. }
  113.  
  114. void Fl_Clock_Output::value(ulong v) {
  115.   struct tm *timeofday;
  116.   timeofday = localtime((const time_t *)&v);
  117.   value(timeofday->tm_hour, timeofday->tm_min, timeofday->tm_sec);
  118. }
  119.  
  120. Fl_Clock_Output::Fl_Clock_Output(int x, int y, int w, int h, const char *l)
  121. : Fl_Widget(x, y, w, h, l) {
  122.   box(FL_UP_BOX);
  123.   selection_color(fl_gray_ramp(5));
  124.   align(FL_ALIGN_BOTTOM);
  125.   hour_ = 0;
  126.   minute_ = 0;
  127.   second_ = 0;
  128.   value_ = 0;
  129. }
  130.  
  131. ////////////////////////////////////////////////////////////////
  132.  
  133. Fl_Clock::Fl_Clock(int x, int y, int w, int h, const char *l)
  134.   : Fl_Clock_Output(x, y, w, h, l) {}
  135.  
  136. Fl_Clock::Fl_Clock(uchar t, int x, int y, int w, int h, const char *l)
  137.   : Fl_Clock_Output(x, y, w, h, l) {
  138.   type(t);
  139.   box(t==FL_ROUND_CLOCK ? FL_NO_BOX : FL_UP_BOX);
  140. }
  141.  
  142. static void tick(void *v) {
  143. #ifdef WIN32
  144.   ((Fl_Clock*)v)->value(time(0));
  145.   Fl::add_timeout(1.0, tick, v);
  146. #else
  147.   struct timeval t;
  148.   gettimeofday(&t, 0);
  149.   ((Fl_Clock*)v)->value(t.tv_sec);
  150.   double delay = 1.0-t.tv_usec*.000001;
  151.   if (delay < .1 || delay > .9) delay = 1.0;
  152.   Fl::add_timeout(delay, tick, v);
  153. #endif
  154. }
  155.  
  156. int Fl_Clock::handle(int event) {
  157.   switch (event) {
  158.   case FL_SHOW:
  159.     tick(this);
  160.     break;
  161.   case FL_HIDE:
  162.     Fl::remove_timeout(tick, this);
  163.     break;
  164.   }
  165.   return Fl_Clock_Output::handle(event);
  166. }
  167.   
  168. Fl_Clock::~Fl_Clock() {
  169.   Fl::remove_timeout(tick, this);
  170. }
  171.  
  172. //
  173. // End of "$Id: Fl_Clock.cxx,v 1.8.2.1 1999/03/25 15:26:42 mike Exp $".
  174. //
  175.